package dao.impl;

import dao.UserDao;
import entity.User;
import entity.User_Inf;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import util.C3P0Util;


import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class UserDaoImpl implements UserDao{
	
	//设计方法用于返回操作数据库的qr对象
	private QueryRunner getQueryRunner(){
		return new QueryRunner(C3P0Util.getDataSource());
	}
	
	@Override
	public int addUser(User user) {
		QueryRunner qr = this.getQueryRunner();
		int i = 0;
		String sql = "insert into user_login values(null,?,?,?,?)";
		Object[] params = {user.getUsername(),user.getPassword(),user.getUser_status(),user.getModified_time()};
		try {
			i = qr.update(sql, params);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return i;
	}

	
	@Override
	public User queryUserByLoginNameAndPassword(String loginName, String password) {
		User user = null;
		QueryRunner qr = this.getQueryRunner();
		String sql = "select * from user_login where username = ? and password=?";
		Object[] params = {loginName,password};
		try {
			user = qr.query(sql, params, new BeanHandler<User>(User.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return user;
	}

	@Override
	public User queryUserByName(String username) {
		User user = null;
		QueryRunner qr = this.getQueryRunner();
		String sql = "select * from user_login where username = ? ";
		try {
			user = qr.query(sql, username,new BeanHandler<User>(User.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return user;
	}

	
	@Override
	public User_Inf queryUserByUsername(String userName) {
		User_Inf user = null;
		QueryRunner qr = this.getQueryRunner();
		String sql = "select * from user_inf where username = ? ";
		try {
			user = qr.query(sql, userName,new BeanHandler<User_Inf>(User_Inf.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return user;
	}

	@Override
	public List<User> selectAllUser() {
		QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
		List<User> uu= null;
		String sql = "select username from user_login ";
		try {
			uu=qr.query(sql, new BeanListHandler<User>(User.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return uu;
	}

	@Override
	public boolean savaUser(User user) {
		
		boolean  flag =false;
		
	       QueryRunner qr = new  QueryRunner(C3P0Util.getDataSource());
	       String sql="insert into user_login values(null,?,?,1,?)";
	    try {
	    	   int i = qr.update(sql,user.getUsername(),user.getPassword(),new Date());
	    	if(i>0){
				flag=true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    return flag;
	}

	@Override
	public boolean saveUserOtherInfo(User_Inf user_inf) {
		boolean  flag =false;
		
	       QueryRunner qr = new  QueryRunner(C3P0Util.getDataSource());
	       String sql="insert into user_inf values(null,null,?,?,?,?,0,1,?,1)";
	    try {
	    	   int i = qr.update(sql,user_inf.getUsername(),
	    			   user_inf.getPhone(),user_inf.getUser_email(),user_inf.getGender(),new Date());
	    	if(i>0){
				flag=true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    return flag;
	}

	

	
}
